package com.lightsh.common;

import java.util.ArrayList;
import java.util.List;

import com.lightsh.utility.Constants;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;

public class CharacterFillHelper {
	private static final String TAG = "CharacterFillHelper";
	private static final boolean DEBUG = Constants.DEBUG;
	
	public static final SpannableStringBuilder getCharaterString(Context context, String text, List<CharacterItem> bitmapList){
		SpannableStringBuilder builder = new SpannableStringBuilder();
		
		if(text == null || text.length() == 0){
    		for(CharacterItem item : bitmapList){
    			appendBitmapToString(context, builder, item.mBitmap);
    		}
    	}else{
    		// 用split不行
    		ArrayList<String> characterStrList = new ArrayList<String>();
    		final int flagLength = Constants.PIC_FLAG.length();
    		int startIndex = 0;
    		int endIndex = text.indexOf(Constants.PIC_FLAG);
    		while(endIndex != -1){
    			characterStrList.add(text.substring(startIndex, endIndex));
    			startIndex = endIndex + flagLength;
    			endIndex = text.indexOf(Constants.PIC_FLAG, startIndex);
    		}
    		if(startIndex < text.length()){
    			characterStrList.add(text.substring(startIndex));
    		}
    		
    		if(characterStrList.size() != bitmapList.size() + 1 && characterStrList.size() != bitmapList.size()){
    			Log.w(TAG, "strCharacter not equal!");
    		}
    		int i = 0;
    		while(i < characterStrList.size() || i < bitmapList.size()){
    			if(i < characterStrList.size()){
    				appendStringToString(builder, characterStrList.get(i));
    			}
    			if(i < bitmapList.size()){
    				appendBitmapToString(context, builder, bitmapList.get(i).mBitmap);
    			}
    			i++;
    		}
    	}		
		return builder;
	}
	public static final SpannableStringBuilder appendBitmapToString(Context context, SpannableStringBuilder builder, Bitmap bitmap){
		return appendBitmapToString(context, builder, builder.length(), bitmap);
	}
	public static final SpannableStringBuilder appendBitmapToString(Context context, SpannableStringBuilder builder, int appendPos, Bitmap bitmap){
		final int startPos = appendPos;
    	builder.insert(startPos, Constants.PIC_FLAG);
    	Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
    	drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
    	ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
    	builder.setSpan(imageSpan, startPos, startPos + Constants.PIC_FLAG.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);		
		return builder;
	}
	public static final SpannableStringBuilder appendStringToString(SpannableStringBuilder builder, String text){
		return appendStringToString(builder, builder.length(), text);
	}
	public static final SpannableStringBuilder appendStringToString(SpannableStringBuilder builder, int appendPos, String text){
		final int startPos = appendPos;
    	builder.insert(startPos, text);
		return builder;
	}
	
	private static void log(String tip){
    	if(DEBUG){
    		Log.d(TAG, "" + tip);
    	}    	
    }
}
